package com.sl.hx.shop.ui.collect.presenter;

import android.content.Context;

import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.CollectList;
import com.sl.hx.shop.bean.ProductList;
import com.sl.hx.shop.bean.RelationBean;
import com.sl.hx.shop.constant.AppConfig;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.ApiRetrofit;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.collect.contacts.CollectListContacts;
import com.sl.hx.shop.ui.home.contacts.GoodsListContacts;
import com.sl.hx.shop.utils.SPUtils;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.List;

import io.reactivex.Observable;

public class CollectListPresenter extends BasePresenter<CollectListContacts.view> implements CollectListContacts.presenter {

    private Context mContext;
    private Boolean isSucess=false;

    public CollectListPresenter(Context context) {
        this.mContext=context;

    }

    //请求商品liebiao
    public void getCollectList(int page, int num) {
        Observable observable = ApiFactory.getInstance().getCollectList(SPUtils.getInstance().getString(AppConfig.ID),page,num);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<List<CollectList>>(mContext) {
            @Override
            protected void _onNext(List<CollectList> collectList) {
                getView().ShowGoodsId(collectList);
            }
            @Override
            protected void _onError(String message) {
            }
        }, false,getView(), FragmentEvent.DESTROY);
    }

    public void delCollect(String num_iid) {
        Observable observable = ApiFactory.getInstance().delCollect(SPUtils.getInstance().getString(AppConfig.ID),num_iid);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<String>(mContext) {
            @Override
            protected void _onNext(String s) {
            }
            @Override
            protected void _onError(String message) {
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().delCollectSuccess();
            }
        }, true,getView(), FragmentEvent.DESTROY);
    }

    public void getRelation(String num_iid) {
        Observable observable = ApiFactory.getInstance().getRelation(ApiRetrofit.getCacheControl(),num_iid,1);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<List<RelationBean>>(mContext) {
            @Override
            protected void _onNext(List<RelationBean> relationBeans) {
                getView().ShowRelation(relationBeans);
                isSucess=true;
            }

            @Override
            protected void _onError(String message) {
            }

            @Override
            public void onComplete() {
                super.onComplete();
                if (!isSucess){
                    getView().ShowRelationNoData();
                }
            }
        }, true,getView(), FragmentEvent.DESTROY);
    }
}
